package cn.wei.sdd.core.service.impl;

import cn.wei.common.exception.Assert;
import cn.wei.common.result.ResponseEnum;
import cn.wei.sdd.core.enums.TransStatusEnum;
import cn.wei.sdd.core.mapper.DictMapper;
import cn.wei.sdd.core.mapper.TransFlowMapper;
import cn.wei.sdd.core.mapper.WarehouseProductMapper;
import cn.wei.sdd.core.pojo.entity.OutWarehouse;
import cn.wei.sdd.core.mapper.OutWarehouseMapper;
import cn.wei.sdd.core.pojo.entity.TransFlow;
import cn.wei.sdd.core.pojo.entity.WarehouseProduct;
import cn.wei.sdd.core.pojo.query.OutWarehouseQuery;
import cn.wei.sdd.core.pojo.vo.OutWarehouseListVO;
import cn.wei.sdd.core.pojo.vo.OutWarehouseVO;
import cn.wei.sdd.core.service.OutWarehouseService;
import cn.wei.sdd.core.utils.NoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 出库记录表 服务实现类
 * </p>
 *
 * @author Wei
 * @since 2021-11-04
 */
@Service
public class OutWarehouseServiceImpl extends ServiceImpl<OutWarehouseMapper, OutWarehouse> implements OutWarehouseService {

    @Resource
    private WarehouseProductMapper warehouseProductMapper;

    @Resource
    private TransFlowMapper transFlowMapper;

    @Resource
    private DictMapper dictMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void outWarehouse(OutWarehouseVO outWarehouseVO, Long userId) {
        Double outNum = outWarehouseVO.getOutNum();
        QueryWrapper<WarehouseProduct> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("dict_id",outWarehouseVO.getDictId());
        WarehouseProduct warehouseProduct = warehouseProductMapper.selectOne(productQueryWrapper);
        Double currentNum = warehouseProduct.getCurrentNum();
        Assert.isTrue(outNum<currentNum, ResponseEnum.CurrentNum_not_enough);

        //减少库存
        UpdateWrapper<WarehouseProduct> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dict_id",outWarehouseVO.getDictId()).set("current_num",currentNum-outNum);
        warehouseProductMapper.update(null,updateWrapper);

        //添加出库记录
        BigDecimal price = outWarehouseVO.getPrice();
        BigDecimal out = new BigDecimal(outNum);
        String parentName = dictMapper.getParentNameByDictId(outWarehouseVO.getDictId());

        BigDecimal TotalPrice = price.multiply(out);
        OutWarehouse outWarehouse = new OutWarehouse();
        outWarehouse.setOutNo(NoUtils.getOutNo());
        outWarehouse.setOperatorId(userId);
        outWarehouse.setDictId(outWarehouseVO.getDictId());
        outWarehouse.setPrice(price);
        outWarehouse.setOutNum(outNum);
        outWarehouse.setTotalPrice(TotalPrice);
        outWarehouse.setParentName(parentName);
        baseMapper.insert(outWarehouse);

        //添加流水
        TransFlow transFlow = new TransFlow();
        transFlow.setUserType(0L);
        transFlow.setUserName("system");
        transFlow.setTransType(TransStatusEnum.SELL.getStatus());
        transFlow.setTransTypeName(TransStatusEnum.SELL.getMsg());
        transFlow.setTransAmount(TotalPrice);
        transFlow.setMemo("销售收入：出库单号"+outWarehouse.getOutNo());
        transFlow.setTransNo(NoUtils.getTransNo());
        transFlowMapper.insert(transFlow);

    }

    @Override
    public IPage<OutWarehouseListVO> listPage(Page<OutWarehouseListVO> pageParam, OutWarehouseQuery outWarehouseQuery) {
        if(outWarehouseQuery == null){
            return baseMapper.mySelectPage(pageParam,null);

        }else{
            return baseMapper.mySelectPage(pageParam,outWarehouseQuery);
        }


    }
}
